package br.ufmg.vod.wbr.algorithims.cluster;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import junit.framework.TestCase;
import br.ufmg.vod.wbr.graphs.FastGraph;
import cern.colt.Arrays;
import edu.uci.ics.jung.algorithms.metrics.Metrics;
import edu.uci.ics.jung.graph.SparseGraph;

public class ClusteringCoefficientTest extends TestCase {


	protected void setUp() throws Exception {
		super.setUp();
	}

	public void testMyRun0() throws IOException {

		SparseGraph<Integer, Object> g = new SparseGraph<Integer, Object>();
		g.addVertex(0);
		g.addVertex(1);
		g.addVertex(2);
		g.addVertex(3);
		
		g.addEdge(new Object(), 0, 1);
		g.addEdge(new Object(), 0, 2);
		g.addEdge(new Object(), 0, 3);
		g.addEdge(new Object(), 1, 2);
		g.addEdge(new Object(), 1, 3);
		g.addEdge(new Object(), 2, 3);

		Map<Integer, Double> clusteringCoefficients = Metrics.clusteringCoefficients(g);
		for (double u : clusteringCoefficients.values()) {
			assertEquals(1.0, u);
		}
	}
	
	public void testMyRun() throws IOException {

		int[][] g = new int[4][];
		for (int i = 0; i < g.length; i++) {
			g[i] = new int[3];
		}
		
		g[0] =  new int[] {1, 2, 3};
		g[1] =  new int[] {0, 2, 3};
		g[2] =  new int[] {1, 0, 3};
		g[3] =  new int[] {1, 2, 0};

		FastGraph fg = new FastGraph(g);
		ClusteringCoefficient clusteringCoefficient = new ClusteringCoefficient(fg);
		clusteringCoefficient.run();
		for (double u : clusteringCoefficient.res) {
			assertEquals(1.0, u);
		}
	}

	public void testMyRun2() throws IOException {

		int[][] g = new int[3][];
		for (int i = 0; i < g.length; i++) {
			g[i] = new int[2];
		}
		
		g[0] =  new int[] {1, 2};
		g[1] =  new int[] {0, 2};
		g[2] =  new int[] {1, 0};

		FastGraph fg = new FastGraph(g);
		ClusteringCoefficient clusteringCoefficient = new ClusteringCoefficient(fg);
		clusteringCoefficient.run();
		for (double u : clusteringCoefficient.res) {
			assertEquals(1.0, u);
		}
	}

	public void testMyRun3() throws IOException {

		int[][] g = new int[4][];
		for (int i = 0; i < g.length; i++) {
			g[i] = new int[3];
		}
		
		g[0] =  new int[] {1, 2};
		g[1] =  new int[] {0, 2, 3};
		g[2] =  new int[] {1, 0, 3};
		g[3] =  new int[] {1, 2};

		FastGraph fg = new FastGraph(g);
		ClusteringCoefficient clusteringCoefficient = new ClusteringCoefficient(fg);
		clusteringCoefficient.run();
		
		System.out.println(Arrays.toString(clusteringCoefficient.res));
		assertEquals(1.0, clusteringCoefficient.res[0]);
		assertEquals(0.6666, clusteringCoefficient.res[1], 0.01);
		assertEquals(0.6666, clusteringCoefficient.res[2], 0.01);
		assertEquals(1.0, clusteringCoefficient.res[3]);
	}
	
	public void testMyRun3_1() throws IOException {

		int[][] g = new int[4][];
		for (int i = 0; i < g.length; i++) {
			g[i] = new int[3];
		}
		
		g[0] =  new int[] {1, 2};
		g[1] =  new int[] {0, 2, 3};
		g[2] =  new int[] {1, 0, 3};
		g[3] =  new int[] {1, 2};

		SparseGraph<Integer, Object> jg = new SparseGraph<Integer, Object>();
		jg.addVertex(0);
		jg.addVertex(1);
		jg.addVertex(2);
		jg.addVertex(3);
		
		jg.addEdge(new Object(), 0, 1);
		jg.addEdge(new Object(), 0, 2);
		jg.addEdge(new Object(), 1, 2);
		jg.addEdge(new Object(), 1, 3);
		jg.addEdge(new Object(), 2, 3);

		Map<Integer, Double> clusteringCoefficients = Metrics.clusteringCoefficients(jg);
		
		FastGraph fg = new FastGraph(g);
		ClusteringCoefficient clusteringCoefficient = new ClusteringCoefficient(fg);
		clusteringCoefficient.run();
		
		System.out.println(Arrays.toString(clusteringCoefficient.res));
		assertEquals(1.0, clusteringCoefficient.res[0]);
		assertEquals(0.6666, clusteringCoefficient.res[1], 0.01);
		assertEquals(0.6666, clusteringCoefficient.res[2], 0.01);
		assertEquals(1.0, clusteringCoefficient.res[3]);
		
		for (Entry<Integer, Double> e : clusteringCoefficients.entrySet()) {
			assertEquals(e.getValue(), clusteringCoefficient.res[e.getKey()], 0.001 );
		}
	}
	
	
	public void testMyRun4() throws IOException {

		int[][] g = new int[4][];
		for (int i = 0; i < g.length; i++) {
			g[i] = new int[3];
		}
		
		g[0] =  new int[] {};
		g[1] =  new int[] {};
		g[2] =  new int[] {};
		g[3] =  new int[] {};

		FastGraph fg = new FastGraph(g);
		ClusteringCoefficient clusteringCoefficient = new ClusteringCoefficient(fg);
		clusteringCoefficient.run();
		
		System.out.println(Arrays.toString(clusteringCoefficient.res));
		for (double u : clusteringCoefficient.res) {
			assertEquals(0.0, u);
		}
	}

	public void testMyRun5() throws IOException {

		int[][] g = new int[5][];
		g[0] =  new int[] {1, 2, 3};
		g[1] =  new int[] {0, 2, 3};
		g[2] =  new int[] {1, 0, 3};
		g[3] =  new int[] {1, 2, 0, 4};
		g[4] =  new int[] {3};

		SparseGraph<Integer, Object> jg = new SparseGraph<Integer, Object>();
		jg.addVertex(0);
		jg.addVertex(1);
		jg.addVertex(2);
		jg.addVertex(3);
		jg.addVertex(4);
		
		jg.addEdge(new Object(), 0, 1);
		jg.addEdge(new Object(), 0, 2);
		jg.addEdge(new Object(), 0, 3);
		jg.addEdge(new Object(), 1, 2);
		jg.addEdge(new Object(), 1, 3);
		jg.addEdge(new Object(), 2, 3);
		jg.addEdge(new Object(), 3, 4);

		Map<Integer, Double> clusteringCoefficients = Metrics.clusteringCoefficients(jg);
		
		FastGraph fg = new FastGraph(g);
		ClusteringCoefficient clusteringCoefficient = new ClusteringCoefficient(fg);
		clusteringCoefficient.run();
		
		for (Entry<Integer, Double> e : clusteringCoefficients.entrySet()) {
			assertEquals(e.getValue(), clusteringCoefficient.res[e.getKey()], 0.001 );
		}
	}
}
